<html>
<head><meta charset="utf-8"><title>Using specialization to differentiate references and values · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html">Using specialization to differentiate references and values</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243571195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243571195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> StackDoubleFlow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243571195">(Jun 22 2021 at 19:59)</a>:</h4>
<p>Hi, I've got a question regarding specialization. I have <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=094a65ade5170a4854f78b539e6f9038">this code</a> that is intended to print </p>
<div class="codehilite"><pre><span></span><code>value
ref
</code></pre></div>
<p>but it fails to compile. How would I do this, and if it requires the unsound specialization feature, how would it be done in theory?</p>



<a name="243572376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243572376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243572376">(Jun 22 2021 at 20:09)</a>:</h4>
<p>Well, the error message is odd. </p>
<blockquote>
<p>= note: downstream crates may implement trait <code>t::Type</code> for type <code>&amp;mut _</code></p>
</blockquote>
<p>That can't happen for a private trait, I think. Unless there's some issue with private traits leaking somewhere.</p>



<a name="243572660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243572660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> StackDoubleFlow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243572660">(Jun 22 2021 at 20:11)</a>:</h4>
<p>Right, this is still open: <a href="https://github.com/rust-lang/rust/issues/48869">https://github.com/rust-lang/rust/issues/48869</a><br>
Although, I do want Type to be public if it can be</p>



<a name="243573760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243573760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243573760">(Jun 22 2021 at 20:21)</a>:</h4>
<p>Well, I tried with min_specialization and adding a marker trait, that would have worked if it weren't for the associated type.</p>
<p>With the associated type I only got it to work with full specialization which is problematic and most likely not what you want.<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=375b1e2881bf5aad2add887e469793ae">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=375b1e2881bf5aad2add887e469793ae</a></p>



<a name="243574037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243574037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243574037">(Jun 22 2021 at 20:24)</a>:</h4>
<p>Maybe if you separate your specialization from the trait's basic definition with associated type you could make it work</p>



<a name="243576015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243576015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243576015">(Jun 22 2021 at 20:40)</a>:</h4>
<p>simplified case without associated type that works with min specialization: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=2a8e0d37ed7169b0face644dc61ebaec">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=2a8e0d37ed7169b0face644dc61ebaec</a></p>



<a name="243576152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243576152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243576152">(Jun 22 2021 at 20:42)</a>:</h4>
<p>the 2nd <code>#[rustc_specialization_trait]</code> isn't needed.</p>



<a name="243609884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243609884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> StackDoubleFlow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243609884">(Jun 23 2021 at 06:00)</a>:</h4>
<p>Thanks for your help! This is valuable information.</p>



<a name="243721249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243721249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> StackDoubleFlow <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243721249">(Jun 23 2021 at 22:31)</a>:</h4>
<p>Would it be possible to have it fail to compile if you tried to use a <code>&amp;mut</code>?</p>



<a name="243965532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Using%20specialization%20to%20differentiate%20references%20and%20values/near/243965532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Using.20specialization.20to.20differentiate.20references.20and.20values.html#243965532">(Jun 25 2021 at 20:16)</a>:</h4>
<p>Depends on what your goal is, would handing out &amp;T do the job?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>